﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace BaseControls
{
    public partial class Clock : UserControl
    {
        private bool isStarted = false;

        public Clock()
        {
            InitializeComponent();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1.0);
            timer.Tick += new EventHandler(timer_Tick);
        }

        public void Pause()
        {
            timer.Stop();
        }
        public void UnPause()
        {
            timer.Start();
        }
        private int maxTime = 0;
        private int tick = 1;
        private DispatcherTimer timer;

        public event TimeElapsedDelegate TimeElapsed;

        public static readonly DependencyProperty TimeProperty =
            DependencyProperty.Register("Time", typeof(int), typeof(Clock), new PropertyMetadata(0));

        public int Time
        {
            get { return (int)GetValue(TimeProperty); }
            set { SetValue(TimeProperty, value); Invalidate(); }
        } 


        public void Start()
        {
            if (Time != 0)
            {
                maxTime = Time;
                RotateTransform trans = new RotateTransform();                
                trans.Angle = TimeToRad(Time);
                if (maxTime >= 60)
                    tick = maxTime / 60;
                else
                    tick = 1;
                Wsk.RenderTransform = trans;
                if (maxTime > 60)
                    timer.Interval = TimeSpan.FromSeconds(tick);
                timer.Start();
                this.Alarm.Begin();
                isStarted = true;
            }
        }
        private double TimeToRad(int seconds)
        {
            return (6/tick) * seconds;
        }
        public int StopTimer()
        {
            RotateTransform trans = new RotateTransform();
            trans.Angle = 0;            
            Wsk.RenderTransform = trans;
            timer.Stop();
            redImage.Opacity = .0;
            isStarted = false;
            return Time;
        }

        void timer_Tick(object sender, EventArgs e)
        {            
            Time -= tick;
            Invalidate();
        }
        void Invalidate()
        {
            RotateTransform trans = new RotateTransform();
            trans.Angle = TimeToRad(Time);
            Wsk.RenderTransform = trans;
            //if ((Time >= 0) && ((Time % 5) == 0)) this part of code start shake timer what is temporary blocked
            //    this.ClockShake.Begin();                
            if (Time > 0 && Time <= 20 && RedClockShow.GetCurrentState() == ClockState.Stopped)//20 seconds before end raise red clock
            {
                this.Alarm.Begin();
                this.RedClockShow.Begin();
            }
            if (Time <= 0 && isStarted)
            {
                if (TimeElapsed != null)
                    TimeElapsed();
                StopTimer();
            }
        }
    }
}
